package com.qf.mywatch.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qf.mywatch.R;
import com.qf.mywatch.model.Data2;
import com.qf.mywatch.model.Data3;
import com.qianfeng.utils.BitMapAsyncTask;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/12/19.
 */

public class GridViewAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Data3> list;

    public GridViewAdapter(Context context, ArrayList<Data3> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder vh;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item3, null);
            vh = new ViewHolder();
            vh.tv1 = (TextView) convertView.findViewById(R.id.item3_tv_title);
            vh.iv = (ImageView) convertView.findViewById(R.id.item3_iv);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        vh.tv1.setText(list.get(position).title);
        vh.iv.setImageResource(R.mipmap.add_brand);
        vh.iv.setTag(list.get(position).img);
        if (!TextUtils.isEmpty(list.get(position).img)) {
            new BitMapAsyncTask(context, new BitMapAsyncTask.CallBack() {
                @Override
                public void senBit(Bitmap bitmap, String s) {
                    if (s.equals(vh.iv.getTag())) {
                        vh.iv.setImageBitmap(bitmap);
                    }
                }
            }).execute(list.get(position).img);
        }
        return convertView;
    }

    class ViewHolder {
        TextView tv1;
        ImageView iv;
    }
}
